/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ZoneConnectionsPropertyEditorPanel.java
 *
 * Created on Aug 13, 2011, 12:29:16 AM
 */
package pt.utl.ist.isr.livetrack.ui.propedit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import org.openide.explorer.propertysheet.PropertyEnv;
import pt.utl.ist.isr.livetrack.server.LTServer;
import pt.utl.ist.isr.livetrack.server.interfaces.Zone;
import pt.utl.ist.isr.livetrack.ui.ZoneNode;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class ZoneConnectionsPropertyEditorPanel extends javax.swing.JPanel {

    private final Zone zone;
    private final LTServer server;
    private final ArrayList<Zone> other;

    ZoneConnectionsPropertyEditorPanel(PropertyEnv pe) {
        initComponents();
        ZoneNode node = (ZoneNode) pe.getBeans()[0];
        this.zone = node.getZone();
        this.server = node.getServer();
        other = new ArrayList<Zone>(node.getServer().getPerimeter().getZones().values());
        Collections.sort(other, new Comparator<Zone>() {

            @Override
            public int compare(Zone o1, Zone o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        other.remove(node.getServer().getPerimeter().getZones().get(node.getZone().getId()));
        ArrayList<String> names = new ArrayList<String>();
        for (Zone z : other) {
            names.add(z.getName());
        }
        connList.setListData(names.toArray());
        ArrayList<Integer> selected = new ArrayList<Integer>();
        for (Zone z : other) {
            for (String zId : zone.getConnections()) {
                Zone zone1 = server.getPerimeter().getZones().get(zId);
                if (z.getId().equals(zone1.getId())) {
                    selected.add(other.indexOf(z));
                }
            }
        }
        int[] indexes = new int[selected.size()];
        for (int i = 0; i < selected.size(); i++) {
            indexes[i] = selected.get(i);
        }
        connList.setSelectedIndices(indexes);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        connList = new javax.swing.JList();

        connList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        connList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                connListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(connList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void connListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_connListValueChanged
        boolean[] selected = new boolean[other.size()];
        for (int i : connList.getSelectedIndices()) {
            selected[i] = true;
        }
        for (int i = 0; i < other.size(); i++) {
            if (selected[i]) {
                if (!zone.getConnections().contains(other.get(i).getId())) {
                    server.getPerimeter().connect(zone.getId(), other.get(i).getId());
                }
            } else if (!selected[i]) {
                if (zone.getConnections().contains(other.get(i).getId())) {
                    server.getPerimeter().disconnect(zone.getId(), other.get(i).getId());
                }
            }
        }
    }//GEN-LAST:event_connListValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList connList;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
